import java.util.*;

public class Main {

    public static int n = 3;
    public static List<Integer> list = new ArrayList<>();
    public static int[] dx = new int[]{1, -1, 0, 0};
    public static int[] dy = new int[]{0, 0, 1, -1};
    public static Queue<int[]> queue = new LinkedList<>();
    public static boolean[][] flag;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int[][] arr = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr[i][j] = sc.nextInt();
                if (arr[i][j] == 0) {
                    queue.offer(new int[]{i, j});
                }
            }
        }

        flag = new boolean[n][n];

//        int ret = Integer.MAX_VALUE;
//        for (int i : list) {
//            ret = Math.min(ret, i);
//        }
//        System.out.println(ret);

        System.out.println(dfs(arr));

        sc.close();
    }

    public static int dfs(int[][] arr) {
        int count = 0;

        while (!queue.isEmpty()) {
            int size = queue.size();
            count++;
            while (size > 0) {
                size--;
                int[] index = queue.poll();
                int x = index[0];
                int y = index[1];
                flag[x][y] = true;
                arr[x][y] ^= 1; //取反
                for (int i = 0; i < 4; i++) {
                    int xx = x + dx[i];
                    int yy = y + dy[i];
                    if (xx >= 0 && xx < n && yy >= 0 && yy < n) {
                        arr[xx][yy] ^= 1; //取反
                        if (arr[xx][yy] == 0) {
                            flag[xx][yy] = false;
                        }
                    }
                }
                if (isAllOpen(arr)) {
                    return count + 1;
                }
            }
        }
        return -1;
    }


    public static boolean isAllOpen(int[][] arr) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (arr[i][j] != 1) {
                    return false;
                }
            }
        }
        return true;
    }
}
